/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scheduler.view.cells;

import java.text.DecimalFormat;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.shape.StrokeType;
import scheduler.core.task.VirtualTaskPool;

/**
 *
 * @author Dima
 */
public class VirtualPoolCell extends ListCell<VirtualTaskPool> {
    private class VirtualPoolGraphic extends GridPane {

        private final String label0Text = "Пріоритет пула задач";
        private final String label1Text = "Кількість задач";
        private final String label2Text = "Середня інтенсивність";
        private final String label3Text = "Середній час виконання";

        public VirtualPoolGraphic(VirtualTaskPool task) {
            setPrefWidth(206);
            setPrefHeight(90);
            setLayoutX(0);
            setLayoutY(0);
            Label key0 = new Label(label0Text);
            Label key1 = new Label(label1Text);
            Label key2 = new Label(label2Text);
            Label key3 = new Label(label3Text);
            Label value0 = new Label(String.valueOf(task.getPrior()));
            Label value1 = new Label(String.valueOf(task.getSize()));
            
            DecimalFormat formatter = new DecimalFormat("###.####");
            
            Label value2 = new Label(formatter.format(task.getLambda()));
            Label value3 = new Label(formatter.format(task.getNu()));
            getChildren().addAll(key0, key1, key2, key3, value0, value1, value2, value3);
            
            Insets left5 = new Insets(0, 0, 0, 5);
            
            GridPane.setConstraints(key0, 1, 0);            
            GridPane.setMargin(key0, left5);
            GridPane.setConstraints(key1, 1, 1);
            GridPane.setMargin(key1, left5);
            GridPane.setConstraints(key2, 1, 2);
            GridPane.setMargin(key2, left5);
            GridPane.setConstraints(key3, 1, 3);
            GridPane.setMargin(key3, left5);
            GridPane.setConstraints(value0, 2, 0);            
            GridPane.setMargin(value0, left5);
            GridPane.setConstraints(value1, 2, 1);
            GridPane.setMargin(value1, left5);
            GridPane.setConstraints(value2, 2, 2);
            GridPane.setMargin(value2, left5);
            GridPane.setConstraints(value3, 2, 3);
            GridPane.setMargin(value3, left5);
            
            Rectangle r1 = getRect(), r2 = getRect(), r3 = getRect(), r4 = getRect();
            getChildren().addAll(r1, r2, r3, r4);
            GridPane.setConstraints(r1, 0, 0);            
            GridPane.setConstraints(r2, 0, 1);
            GridPane.setConstraints(r3, 0, 2);
            GridPane.setConstraints(r4, 0, 3);
            
            ObservableList<ColumnConstraints> columnConstraints = getColumnConstraints();
            ColumnConstraints column0 = new ColumnConstraints();
            ColumnConstraints column1 = new ColumnConstraints();
            ColumnConstraints column2 = new ColumnConstraints();
            column0.setHalignment(HPos.CENTER);
            column0.setHgrow(Priority.SOMETIMES);
            column0.setMaxWidth(28);
            column0.setMinWidth(9);
            column0.setPrefWidth(28);
            column1.setHalignment(HPos.LEFT);
            column1.setHgrow(Priority.SOMETIMES);
            column1.setMaxWidth(255);
            column1.setMinWidth(10);
            column1.setPrefWidth(255);
            column2.setHalignment(HPos.CENTER);
            column2.setHgrow(Priority.SOMETIMES);
            column2.setMaxWidth(85);
            column2.setMinWidth(10);
            column2.setPrefWidth(85);
            
            columnConstraints.addAll(column0, column1, column2);
            
            ObservableList<RowConstraints> rowConstraints = getRowConstraints();
            RowConstraints row0 = new RowConstraints();
            RowConstraints row1 = new RowConstraints();
            RowConstraints row2 = new RowConstraints();
            RowConstraints row3 = new RowConstraints();
            row0.setMinHeight(10);
            row0.setPrefHeight(30);
            row0.setVgrow(Priority.SOMETIMES);
            row1.setMinHeight(10);
            row1.setPrefHeight(30);
            row1.setVgrow(Priority.SOMETIMES);
            row2.setMinHeight(10);
            row2.setPrefHeight(30);
            row2.setVgrow(Priority.SOMETIMES);
            row3.setMinHeight(10);
            row3.setPrefHeight(30);
            row3.setVgrow(Priority.SOMETIMES);
            
            rowConstraints.addAll(row0, row1, row2, row3);
        }
        
        private Rectangle getRect() {
            return RectangleBuilder.create().
                                    arcWidth(0).
                                    arcHeight(0).
                                    fill(Color.GREENYELLOW).
                                    width(10).
                                    height(10).
                                    stroke(Color.BLACK).
                                    strokeType(StrokeType.INSIDE).
                                    build();
        }
    }

    @Override
    protected void updateItem(VirtualTaskPool task, boolean empty) {
        super.updateItem(task, empty);

        if (task != null) {
            setGraphic(new VirtualPoolGraphic(task));
        }
    }
}
